<template>
  <el-dialog :close-on-click-modal='false' title="用户信息" :visible.sync="dialogFormVisible" :before-close="cancel">
    <el-form ref="form" :rules="rules" :model="form" label-width="100px">
      <el-row>
        <el-col :span="24">
          <el-form-item label="用户名：" prop="nickName">
            <el-input v-model="form.nickName"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="账号：" prop="userName">
            <el-input v-model="form.userName"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="手机号：" prop="phoneNumber">
            <el-input v-model="form.phoneNumber"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="邮箱：" prop="email">
            <el-input v-model="form.email"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="用户头像：" prop="avatar">
            <Upload ref="uploadImg" :limit="1" :urlList="form.imgList" @getFile="getFile"></Upload>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="cancel">取 消</el-button>
      <el-button type="primary" @click="confirm">确 定</el-button>
    </div>
  </el-dialog>
</template>
<script>
import Upload from "@/components/Upload"
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    formData: {
      type: Object,
      required: false
    },
    api: {
      type: Function,
      required: true
    }
  },
  components: { Upload },
  data () {
    return {
      dialogFormVisible: false,
      rules: {
        email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' },
          {
            validator: function (rule, value, callback) {
              if (
                /^\w{1,64}@[a-z0-9\-]{1,256}(\.[a-z]{2,6}){1,2}$/i.test(
                  value
                ) == false
              ) {
                callback(new Error("邮箱格式错误"));
              } else {
                callback();
              }
            },
            trigger: "blur"
          }
        ],
        phoneNumber: [
          { required: true, message: '请输入手机号码', trigger: 'blur' },
          {
            required: true,
            pattern: /^[1][3,4,5,7,8,9][0-9]{9}$/,
            message: '请输入正确的手机号码',
            trigger: 'blur',
          },
        ],
        userName: [
          { required: true, message: '请输入账号', trigger: 'blur' }
        ],
        nickName: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        avatar: [
          { required: true, message: '请添加用户头像', trigger: 'change' }
        ],
      },
      form: {
        imgList: [],
        avatar: '',
        nickName: '',
        userName: '',
        phoneNumber: '',
        email: '',
        userType: "platform_user"
      },
    };
  },
  watch: {
    'form.avatar' (v) {
      v && this.$refs.form && this.$refs.form.clearValidate(['avatar']);
    },
    showDialog: {
      handler (v) {
        this.dialogFormVisible = v
        v && (this.form = { ...this.form, ...this.formData })
        v && (this.form.imgList = this.form.avatar ? [{ name: this.form.avatar, url: this.form.avatar }] : [])
      }
    }
  },
  methods: {
    // 上传图片
    getFile (file) {
      this.form.avatar = file[0] ? file[0].url : ''
    },
    //关闭 isRef是否刷新表单
    close (isRef) {
      this.$emit('closeDialog', isRef)
      this.$refs.form.resetFields()
    },
    //取消
    cancel () {
      this.form = {
        imgList: [],
        avatar: '',
        nickName: '',
        userName: '',
        phoneNumber: '',
        email: '',
        userType: "platform_user"
      }
      this.close()
    },
    //确认
    confirm () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          //  this.api 添加编辑接口 （注意传参）
          this.api(this.form).then(res => {
            if (res.code == 200) {
              this.$message({
                type: 'success',
                message: '操作成功!'
              });
              this.cancel()
              this.close(true)
            } else {
              this.$message({
                type: 'info',
                message: res.msg || '操作失败!'
              });
            }
          })
        } else {
          return false;
        }
      });
    }
  }
};
</script>